package com.angis.fx.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.angis.fx.data.ChangsuoInformation;
import com.angis.fx.data.ContextInfo;

public class DataParseUtil {
	/**
	 * 解析登录返回信息，转化为实体Bean
	 * @param result
	 * @return
	 */
	public static ContextInfo parseLoginResult(String result){
		return null;
	}
	
	public static List<Map<String, String>> parseCheckInfo(String response){
		List<Map<String, String>> lList = new ArrayList<Map<String,String>>();
		Map<String, String> lMap;
		String[] lInfos = response.split("&amp;");
		for (String lInfo : lInfos){
			lMap = new HashMap<String, String>();
			String[] lParams = lInfo.split("\\^");
			lMap.put("time", lParams[1]);
			lMap.put("person", lParams[0]);
			lMap.put("status", lParams[2]);
			lMap.put("result", lParams[3]);
			lList.add(lMap);
		}
		return lList;
	}
	
	public static List<ChangsuoInformation> parseChangsuoInfo(String response) {
		/**
		 * con_recordid=R050926103038199^x=120.053060^y=30.245910^con_title=
		 * 杭州阿武网吧有限公司
		 * ^permitcode=00071^permitword=杭西网服^address=西湖区留下镇留下大街147号^areatype
		 * =网吧^managepos
		 * =正常营业^area=杭州市西湖区^manager=郑东川^tel=13805724541^areacode=330106
		 * ^hycode=01^code=8071^unittype=157
		 */

		// char[] responseChars = response.toCharArray();
		// for (int i=0; i<responseChars.length; i++){
		// if (responseChars[i] == "^".charAt(0)){
		// responseChars[i] = "*".charAt(0);
		// }
		// }
		// response = new String(responseChars);
		
		List<ChangsuoInformation> lPlaceList = new ArrayList<ChangsuoInformation>();
		String[] lChangsuoInfoStrings = response.split("&amp;");
		ChangsuoInformation lChangsuoInfo;
		String[] params;
		for (String lChangsuoInfoString : lChangsuoInfoStrings) {
			lChangsuoInfo = new ChangsuoInformation();
			params = lChangsuoInfoString.split("\\^");
			if (params[0].split("=").length>1)
				lChangsuoInfo.setCsId(params[0].split("=")[1]);
			if (params[1].split("=").length>1)
				lChangsuoInfo.setLon(Double.parseDouble(params[1].split("=")[1]));
			if (params[2].split("=").length>1)
				lChangsuoInfo.setLat(Double.parseDouble(params[2].split("=")[1]));
			if (params[3].split("=").length>1)
				lChangsuoInfo.setTitle(params[3].split("=")[1]);
			if (params[4].split("=").length>1)
				lChangsuoInfo.setPermitCode(params[4].split("=")[1]);
			if (params[5].split("=").length>1)
				lChangsuoInfo.setPermitWord(params[5].split("=")[1]);
			if (params[6].split("=").length>1)
				lChangsuoInfo.setAddress(params[6].split("=")[1]);
			if (params[7].split("=").length>1)
				lChangsuoInfo.setAreaType(params[7].split("=")[1]);
			if (params[8].split("=").length>1)
				lChangsuoInfo.setManagePos(params[8].split("=")[1]);
			if (params[9].split("=").length>1)
				lChangsuoInfo.setArea(params[9].split("=")[1]);
			if (params[10].split("=").length>1)
				lChangsuoInfo.setPersonInCharge(params[10].split("=")[1]);
			if (params[11].split("=").length>1)
				lChangsuoInfo.setTelefon(params[11].split("=")[1]);
			if (params[12].split("=").length>1)
				lChangsuoInfo.setAreaCode(params[12].split("=")[1]);
			if (params[13].split("=").length>1)
				lChangsuoInfo.setHyCode(params[13].split("=")[1]);
			if (params[14].split("=").length>1)
				lChangsuoInfo.setCode(params[14].split("=")[1]);
			if (params[15].split("=").length>1)
				lChangsuoInfo.setUnitType(params[15].split("=")[1]);
			lPlaceList.add(lChangsuoInfo);
		}
		return lPlaceList;
	}
	
	public static List<ChangsuoInformation> parseSimpleChangsuoInfo(String response, Double pLon, Double pLat) {
		/**
		 * con_recordid=R050926103038199^x=120.053060^y=30.245910^con_title=
		 * 杭州阿武网吧有限公司
		 * ^permitcode=00071^permitword=杭西网服^address=西湖区留下镇留下大街147号^areatype
		 * =网吧^managepos
		 * =正常营业^area=杭州市西湖区^manager=郑东川^tel=13805724541^areacode=330106
		 * ^hycode=01^code=8071^unittype=157
		 */

		// char[] responseChars = response.toCharArray();
		// for (int i=0; i<responseChars.length; i++){
		// if (responseChars[i] == "^".charAt(0)){
		// responseChars[i] = "*".charAt(0);
		// }
		// }
		// response = new String(responseChars);
		
		List<ChangsuoInformation> lPlaceList = new ArrayList<ChangsuoInformation>();
		String[] lChangsuoInfoStrings = response.split("&amp;");
		double[] distances = new double[lChangsuoInfoStrings.length];
		for (int k=0; k<distances.length; k++){
			distances[k] = 10000;
		}
		ChangsuoInformation lChangsuoInfo;
		String[] params;
		for (int i=0; i<lChangsuoInfoStrings.length; i++) {
			lChangsuoInfo = new ChangsuoInformation();
			params = lChangsuoInfoStrings[i].split("\\^");
			if (params[0].split("=").length>1)
				lChangsuoInfo.setCsId(params[0].split("=")[1]);
			if (params[1].split("=").length>1)
				lChangsuoInfo.setLon(Double.parseDouble(params[1].split("=")[1]));
			if (params[2].split("=").length>1)
				lChangsuoInfo.setLat(Double.parseDouble(params[2].split("=")[1]));
			if (params[3].split("=").length>1)
				lChangsuoInfo.setTitle(params[3].split("=")[1]);
			if (params[4].split("=").length>1)
				lChangsuoInfo.setAreaType(params[4].split("=")[1]);
			if (params[5].split("=").length>1)
				lChangsuoInfo.setManagePos(params[5].split("=")[1]);
//			if (params[6].split("=").length>1)
//				lChangsuoInfo.setPermitWord(params[6].split("=")[1]);
			
			if(lChangsuoInfo.getTitle()!=null){
				for (int j=0; j<distances.length; j++){
					double curDistanceSqr = (lChangsuoInfo.getLon()-pLon)*(lChangsuoInfo.getLon()-pLon) + (lChangsuoInfo.getLat()-pLat)*(lChangsuoInfo.getLat()-pLat);
					if (curDistanceSqr < distances[j]){
						lPlaceList.add(j,lChangsuoInfo);
						distances[j] = curDistanceSqr;
						break;
					}
				}
			}
		}
		return lPlaceList;
	}
	
	public static List<ChangsuoInformation> parseSimpleChangsuoInfo(String response) {
		/**
		 * con_recordid=R050926103038199^x=120.053060^y=30.245910^con_title=
		 * 杭州阿武网吧有限公司
		 * ^permitcode=00071^permitword=杭西网服^address=西湖区留下镇留下大街147号^areatype
		 * =网吧^managepos
		 * =正常营业^area=杭州市西湖区^manager=郑东川^tel=13805724541^areacode=330106
		 * ^hycode=01^code=8071^unittype=157
		 */

		// char[] responseChars = response.toCharArray();
		// for (int i=0; i<responseChars.length; i++){
		// if (responseChars[i] == "^".charAt(0)){
		// responseChars[i] = "*".charAt(0);
		// }
		// }
		// response = new String(responseChars);
		
		List<ChangsuoInformation> lPlaceList = new ArrayList<ChangsuoInformation>();
		String[] lChangsuoInfoStrings = response.split("&amp;");
		ChangsuoInformation lChangsuoInfo;
		String[] params;
		for (String lChangsuoInfoString : lChangsuoInfoStrings) {
			lChangsuoInfo = new ChangsuoInformation();
			params = lChangsuoInfoString.split("\\^");
			if (params[0].split("=").length>1)
				lChangsuoInfo.setCsId(params[0].split("=")[1]);
			if (params[1].split("=").length>1)
				lChangsuoInfo.setLon(Double.parseDouble(params[1].split("=")[1]));
			if (params[2].split("=").length>1)
				lChangsuoInfo.setLat(Double.parseDouble(params[2].split("=")[1]));
			if (params[3].split("=").length>1)
				lChangsuoInfo.setTitle(params[3].split("=")[1]);
			if (params[4].split("=").length>1)
				lChangsuoInfo.setAreaType(params[4].split("=")[1]);
			if (params[5].split("=").length>1)
				lChangsuoInfo.setManagePos(params[5].split("=")[1]);
			if (lChangsuoInfo.getTitle()!=null)
				lPlaceList.add(lChangsuoInfo);
		}
		return lPlaceList;
	}
	
	public static ChangsuoInformation parseChangsuoInfo2(String response){
		ChangsuoInformation lChangsuoInfo = new ChangsuoInformation();
		String[] params = response.split("\\^");
		if (params[0].split("=").length>1)
			lChangsuoInfo.setCsId(params[0].split("=")[1]);
		if (params[1].split("=").length>1)
			lChangsuoInfo.setTitle(params[1].split("=")[1]);
		if (params[2].split("=").length>1)
			lChangsuoInfo.setAddress(params[2].split("=")[1]);
		if (params[3].split("=").length>1)
			lChangsuoInfo.setPersonInCharge(params[3].split("=")[1]);
		if (params[4].split("=").length>1)
			lChangsuoInfo.setAreaType(params[4].split("=")[1]);
		if (params[5].split("=").length>1)
			lChangsuoInfo.setPermitCode(params[5].split("=")[1]);
		if (params[6].split("=").length>1)
			lChangsuoInfo.setPermitWord(params[6].split("=")[1]);
		if (params[7].split("=").length>1)
			lChangsuoInfo.setArea(params[7].split("=")[1]);
		if (params[8].split("=").length>1)
			lChangsuoInfo.setTelefon(params[8].split("=")[1]);
		if (params[9].split("=").length>1)
			lChangsuoInfo.setAreaCode(params[9].split("=")[1]);
		if (params[10].split("=").length>1)
			lChangsuoInfo.setHyCode(params[10].split("=")[1]);
		if (params[11].split("=").length>1)
			lChangsuoInfo.setCode(params[11].split("=")[1]);
		if (params[12].split("=").length>1)
			lChangsuoInfo.setUnitType(params[12].split("=")[1]);
		if (params[13].split("=").length>1)
			lChangsuoInfo.setManagePos(params[13].split("=")[1]);
		return lChangsuoInfo;
	}
	
	public static String handleResponse(String response) {
		response = response.substring(0, response.lastIndexOf("<"));
		response = response.substring(response.lastIndexOf(">") + 1);
		return response;
	}

	public static List<Map<String, String>> parsePunishInfo(
			String response) {
		List<Map<String, String>> lList = new ArrayList<Map<String,String>>();
		Map<String, String> lMap;
		String[] lInfos = response.split("&amp;");
		for (String lInfo : lInfos){
			lMap = new HashMap<String, String>();
			String[] lParams = lInfo.split("\\^");
			lMap.put("name", lParams[0]);
			lMap.put("reason", lParams[1]);
			lMap.put("orga", lParams[2]);
			lMap.put("person", lParams[3]);
			lList.add(lMap);
		}
		return lList;
	}
}
